<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="">
      <el-card shadow="hover">
        <div class="user">
          <img src="../../assets/images/person.png" alt="用户头像">
          <div class="user-info">
            <p class="name">{{ userInfo.name }}</p>
            <p class="role">{{ userInfo.role }}</p>
          </div>
        </div>
        <div class="login-info">
          <p><span>{{ userInfo.description }}</span></p>
        </div>
      </el-card>

      <!-- <el-card shadow="hover" style="margin-top: 20px;" height="450px">
          <el-table :data="tableData">
              <el-table-column v-for="(val, key) in tableLabel" :key="key" :prop="key"
                  :label="val"></el-table-column>
          </el-table>
      </el-card> -->
      <el-card class="Introduction">
        <UserCenter  v-if="userInfo.userType===3"/>
        <UpdatePass  v-else/>
      </el-card>
    </el-col>
    <el-col :span="16">
      <!-- <div class="num">
          <el-card :body-style="{ display: 'flex', padding: 0 }" v-for="item in countData" :key="item.name">
              <component class="icons" :is="item.icon" :style="{ background: item.color }">
              </component>
              <div class="detail">
                  <p class="num">$ {{ item.value }}</p>
                  <p class="txt">{{ item.name }}</p>
              </div>
          </el-card>
      </div> -->

      <el-card class="home-data-card"
               :body-style="{  overflow: 'hidden' }">
        <div class="home-system-data" :style="{display:userInfo?.userType!==1?'block':'flex'}">
          <SystemData v-if="userInfo.userType===1"/>
          <CourseTable v-if="userInfo.userType===3"/>
          <CourseTableTeacher v-if="userInfo.userType===2"/>
        </div>
        <!--        <el-carousel height="700px" motion-blur>-->
        <!--          <el-carousel-item v-for="item in 1" :key="item">-->
        <!--            &lt;!&ndash;                        <h3 class="small justify-center" text="2xl">{{ item }}</h3>&ndash;&gt;-->


        <!--          </el-carousel-item>-->
        <!--        </el-carousel>-->
      </el-card>
      <!-- <div class="graph">
          <el-card style="height: 260px;">
              <div ref="userechart" style="height: 240px;"></div>
          </el-card>
          <el-card style="height: 260px;">
              <div ref="videoechart" style="height: 240px;"></div>
          </el-card>
      </div> -->
    </el-col>
  </el-row>
</template>

<script setup>
import {getCurrentInstance, onMounted, reactive, ref} from 'vue';
import * as echarts from 'echarts';
import SystemData from "@/views/admin/SystemData.vue";
import CourseTable from "@/views/course/CourseTable.vue";
import CourseTableTeacher from "@/views/course/CourseTableTeacher.vue";
import UserCenter from "@/views/student/UserCenter.vue";
import UpdatePass from "@/views/user/UpdatePass.vue";

const {proxy} = getCurrentInstance();
let tableData = ref([]);
let countData = ref([]);

let userInfo = ref({});


const getInfo = () => {
  let admin = window.localStorage.getItem('admin');
  let teacher = window.localStorage.getItem('teacher');
  let student = window.localStorage.getItem('student');

  if (admin) {
    userInfo.value = {
      name: (JSON.parse(admin)).realname,
      role: "管理员",
      description: (JSON.parse(admin)).description,
      userType: (JSON.parse(admin)).userType
    }
    return
  }
  if (teacher) {
    userInfo.value = {
      name: (JSON.parse(teacher)).realname,
      role: (JSON.parse(teacher)).jobtitle,
      description: (JSON.parse(teacher)).description,
      userType: (JSON.parse(teacher)).userType
    }
    return
  }
  if (student) {
    userInfo.value = {
      name: (JSON.parse(student)).realname,
      role: "学生",
      description: (JSON.parse(student)).description,
      userType: (JSON.parse(student)).userType
    }
    return
  }

}

const tableLabel = {
  name: '课程',
  todayBuy: '今日购买',
  monthBuy: '本月购买',
  totalBuy: '总购买',
};
const getTableList = async () => {
  let res = await proxy.$api.getTableData();
  tableData.value = res;
};
// 获取首页 Count 数据
const getCountList = async () => {
  let res = await proxy.$api.getCountData();
  // console.log(res);
  countData.value = res;
}

onMounted(() => {
  // getTableList();
  // getCountList();
  // 获取ecahrts 数据
  // getChartData();

  getInfo();
});
// 关于 echarts 表格的渲染部分
let xOptions = reactive({
  legend: {
    // 图例文字颜色
    textStyle: {
      color: "#333",
    },
  },
  grid: {
    left: "20%",
  },
  // 提示框
  tooltip: {
    trigger: "axis",
  },
  xAxis: {
    type: "category", // 类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: "#17b3a3",
      },
    },
    axisLabel: {
      interval: 0,
      color: "#333",
    },
  },
  yAxis: [
    {
      type: "value",
      axisLine: {
        lineStyle: {
          color: "#17b3a3",
        },
      },
    },
  ],
  color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
  series: [],
});
let pieOptions = reactive({
  tooltip: {
    trigger: "item",
  },
  color: [
    "#0f78f4",
    "#dd536b",
    "#9462e5",
    "#a6a6a6",
    "#e1bb22",
    "#39c362",
    "#3ed1cf",
  ],
  series: [],
});
let orderData = reactive({
  xData: [],
  series: [],
});
let userData = reactive({
  xData: [],
  series: [],
});
let videoData = reactive({
  series: [],
})
// 获取数据
const getChartData = async () => {
  let result = await proxy.$api.getChartData();
  // console.log(result);
  let res = result.orderData;
  let userRes = result.userData;
  let videoRes = result.videoData;
  orderData.xData = res.data;
  const keyArray = Object.keys(res.data[0])
  const series = []
  keyArray.forEach((key) => {
    series.push({
      name: key,
      data: res.data.map(item => item[key]),
      type: "line",
    });
  });
  orderData.series = series;
  xOptions.xAxis.data = orderData.xData;
  xOptions.series = orderData.series;
  // userData进行渲染
  let hEcharts = echarts.init(proxy.$refs['echart']);
  hEcharts.setOption(xOptions);

  // 柱状图渲染
  userData.xData = userRes.map((item) => item.date);
  userData.series = [
    {
      name: "新增用户",
      data: userRes.map((item) => item.new),
      type: "bar"
    },
    {
      name: "活跃用户",
      data: userRes.map((item) => item.active),
      type: "bar"
    }
  ];
  xOptions.xAxis.data = userData.xData;
  xOptions.series = userData.series;
  let uEcharts = echarts.init(proxy.$refs['userechart']);
  uEcharts.setOption(xOptions);

  // 饼状图渲染
  videoData.series = [
    {
      data: videoRes,
      type: 'pie',
    }
  ];
  pieOptions.series = videoData.series;
  let vEcharts = echarts.init(proxy.$refs['videoechart']);
  vEcharts.setOption(pieOptions);

}
</script>

<style lang="less" scoped>
.home {
  .user {
    display: flex;
    align-items: center;
    padding-bottom: 20px;
    border-bottom: 1px solid #ccc;
    margin-bottom: 20px;

    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
      margin-right: 40px;
    }
  }

  .login-info {
    p {
      line-height: 30px;
      font-size: 14px;
      color: #999;

      span {
        color: #666;
        margin-left: 60px;
      }
    }
  }

  .num {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-card {
      width: 32%;
      margin-bottom: 20px;
    }

    .icons {
      height: 80px;
      width: 80px;
      font-size: 30px;
      text-align: center;
      line-height: 80px;
      color: #fff;
    }

    .detail {
      margin-left: 15px;
      display: flex;
      flex-direction: column;
      justify-content: center;

      .num {
        font-size: 30px;
        margin-bottom: 10px;
      }

      .txt {
        font-size: 14px;
        text-align: center;
        color: #999;
      }
    }
  }

  .graph {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;

    .el-card {
      width: 48%;
    }
  }
}

.name {
  font-size: 40px;
  font-family: 'Courier New', Courier, monospace;
}

.role {
  margin-top: 20px;
}

.el-carousel__item h3 {
  color: #475669;
  opacity: 0.75;
  line-height: 700px;
  margin: 0;
  text-align: center;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}


.Introduction {
  height: 400px;
  margin-top: 20px;
  text-align: center;
  .update-form{
    width: auto;
    left: auto;
    transform: none;
    position: relative;
    margin-top:0;
  }
}

.home-data-card {
  //margin: 0;
  /* 把滚动条隐藏掉，超出的内容看不到 */
  overflow: hidden;
}

/* 让这个容器宽度永远占满父级，但是不超过它 */
.home-system-data {
  width: 100%;
  max-width: 100%;
  height: 700px;
  box-sizing: border-box;
  //display: flex; /* 打开 Flex 布局 */
  align-items: center; /* 垂直（上下）居中 */
  justify-content: center; /* 水平（左右）居中，按需可去掉 */

  .wrapper {
    position: relative;
    width: 95%;
    height: 95%;
  }
}
</style>